package hmyg

import (
	"bytes"
	"core/datetime"
	"encoding/json"
	"fmt"
	"io"
	entity2 "iotall/iotcore/entity"
	api "iotall/iottest/entity"
	"math/rand"
	"net/http"
	"time"
)

// 10独立设备  20独立多设备   30独立控制器    40用传  50中继器

type Client struct {
	Id   string
	IMSI string

	sinr            uint8
	ecl             int
	battery_voltage float32
	battery_value   int
	ICCID           string

	smoke_state uint8
	smoke_value uint8

	tamper_alarm uint8
	connOk       bool
}

func (p *Client) GetData() *entity2.StdPkg {
	p.sinr = uint8(rand.Intn(10))
	p.ecl = -900 + (rand.Intn(50))
	p.battery_voltage = 3.0 + float32(rand.Intn(10))*0.1
	p.battery_value = 90 + rand.Intn(10)
	p.smoke_value = uint8(rand.Intn(100))
	p.smoke_state = 0
	if p.smoke_value > 95 {
		p.smoke_state = 1
	}
	if rand.Intn(100) > 90 {
		p.tamper_alarm = 1
	} else {
		p.tamper_alarm = 0
	}

	var r = &entity2.StdPkg{
		Sid:     p.Id,
		CrtDt:   datetime.FormatDateTime(time.Now()),
		Events:  make([]entity2.StdPkgEvent, 0),
		Devices: make([]entity2.StdPkgDevice, 0),
	}

	var mdev = entity2.NewMainStdPkgDevice()

	//默认规则  1000  主要监测属性  2000  其他监测属性   3000 固有属性   4000可配置属性
	{
		var m = entity2.NewStdPkgProp("1010", "烟雾浓度", "", entity2.StdPkgProp_statevalue, float32(p.smoke_value))
		m.RaiseEvent = true
		if p.smoke_state == 0 {
			m.State = "正常"
		} else {
			m.State = "一级报警"
		}
		m.Vh = "90"
		m.Unit = "%"
		m.Monitor = true
		if p.Id == "10000000001" {
			m.State = "故障"
		}
		if p.Id == "10000000002" {
			m.State = "一级报警"
		}
		if p.Id == "10000000003" {
			m.State = "二级报警"
		}
		mdev.Props = append(mdev.Props, *m)
	}

	{
		var m = entity2.NewStdPkgProp("2010", "电池电量", "", entity2.StdPkgProp_value, float32(p.battery_value))
		m.Unit = "%"

		mdev.Props = append(mdev.Props, *m)
	}
	{
		var m = entity2.NewStdPkgProp("2020", "电池电压", "", entity2.StdPkgProp_value, float32(p.battery_voltage))
		m.Unit = "V"
		mdev.Props = append(mdev.Props, *m)
	}

	{
		var m = entity2.NewStdPkgProp("2030", "sinr", "", entity2.StdPkgProp_value, float32(p.sinr))
		mdev.Props = append(mdev.Props, *m)
	}
	{
		var m = entity2.NewStdPkgProp("2040", "ecl", "", entity2.StdPkgProp_value, float32(p.ecl))
		mdev.Props = append(mdev.Props, *m)
	}
	{
		var m = entity2.NewStdPkgProp("2050", "防拆状态", "", entity2.StdPkgProp_state, float32(p.tamper_alarm))
		m.RaiseEvent = true
		if p.tamper_alarm == 1 {
			m.State = "正常"
			m.Sv = "正常"
		} else {
			m.State = "故障"
			m.Sv = "被拆"
		}
		mdev.Props = append(mdev.Props, *m)
	}

	{
		var m = entity2.NewStdPkgProp("3010", "IMSI", p.IMSI, entity2.StdPkgProp_fix, 0)
		mdev.Props = append(mdev.Props, *m)
	}

	{
		var m = entity2.NewStdPkgProp("3020", "ICCID", p.ICCID, entity2.StdPkgProp_fix, 0)
		mdev.Props = append(mdev.Props, *m)
	}

	r.Devices = append(r.Devices, *mdev)

	return r
}
func (p *Client) Send() {
	var data = p.GetData()
	postBody, _ := json.Marshal(data)
	// 将数据转换为字节序列
	requestBody := bytes.NewBuffer(postBody)

	// 创建请求

	req, err := http.NewRequest("POST", api.GloabSetting.ServerAddr, requestBody)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 设置请求头信息，指定内容类型和长度
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Content-Length", fmt.Sprintf("%d", requestBody.Len()))

	// 执行请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	api.AddXtUp(1)
	// 读取响应体
	_, err = io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	api.AddXtBack(1)
	if !p.connOk {
		p.connOk = true
		api.AddConnOk(1)
	}
}
func NewClient(sid string) *Client {
	var r = &Client{
		Id:    sid,
		IMSI:  "IMSI" + sid,
		ICCID: "ICCID" + sid,
	}
	return r
}
func (p *Client) Start() {

	for {
		var sleep = rand.Intn(10)
		time.Sleep(time.Second * time.Duration(sleep))
		p.Send()
		time.Sleep(time.Second * time.Duration(api.GloabSetting.HeathDuration))
	}
}

//{ "smoke_value": 4, "smoke_state": 1 }

/*
"payload":{"terminal_type":"JT
Y-GD-CNB","software_version":"A3.2","sinr":29,"rsrp":-1023,"pci":19,"manufacturer_name":"
HIPPO JIANGSU.CHINA ","heartbeat_time":11.0,"hardware_version":"V1.1","ecl":0,"cell_ID"
:204528465,"battery_voltage":2.9100000858306885,"battery_value":90,"IMSI":"460113150701361","IMEI":"869490055324241","ICCID":"89861121207017972542"},"messageType":"dataReport","deviceType":"","deviceId":"80513328f8f14c75a8458c516e600e2d","assocAssetId":"","IMSI":"undefined","IMEI":"869490055324241"}
00:17:06:739 {"timestamp":1711988226616,"tenantId":"2000037018","protocol":"lwm2m

*/
